/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ar.com.wholeconsulting.pacc.gui.utils.treetable.generic;

import ar.com.wholeconsulting.pacc.gui.utils.table.AbstractEditorTableModel;
import ar.com.wholeconsulting.pacc.gui.utils.treetable.TreeTableNode;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.DefaultCellEditor;
import javax.swing.tree.TreeCellEditor;

/**
 *
 * @author Duco Pablo
 */
public class GenericTreeTableNode extends TreeTableNode {

    private static String GET = "get";
    private static String SET = "set";
    private String field;
//
//    public AparatosTreeTableNode(Usuario usuario) {
//        super(usuario.getNombre(), "", usuario);
//        for (Aparato aparato : usuario.getAparatos()) {
//            this.getChildren().add(new TreeTableNode(aparato.getModelo().getNombre(), aparato.getDescripcion(), aparato));
//        }
//    }

    public static TreeTableNode getNodeStruncture(GenericEditorItems items, Map<String, DefaultCellEditor> map) {
        TreeTableNode root = new TreeTableNode(items.getTituloAtributos(), "", items.getTituloAtributos());
        for (int i = 0; i < items.getTreeNames().length; i++) {
            root.getChildren().add(new GenericTreeTableNode(items.getTreeNames()[i], items.getAttributeNames()[i], items.getInstance()));
            if (items.getEditors()[i] != null) {
                map.put(items.getTreeNames()[i], items.getEditors()[i]);
            }
        }
        return root;
    }

    public GenericTreeTableNode(String name, String field, Object obj) {
        super(name, "", obj);
        this.field = field;
    }

    @Override
    public String getName() {
        return super.getName();
    }

    @Override
    public String getDescription() {
        try {
            String method = GET + field;
            Method mt = getMethod(method);
            Object value = mt.invoke(this.getObj());
            if (value == null) {
                return "";
            } else {
                return value.toString();
            }
        } catch (IllegalAccessException ex) {
            Logger.getLogger(AbstractEditorTableModel.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IllegalArgumentException ex) {
            System.out.println("error accediendo a "+GET + field);
            
            Logger.getLogger(AbstractEditorTableModel.class.getName()).log(Level.SEVERE, null, ex);
        } catch (InvocationTargetException ex) {
            Logger.getLogger(AbstractEditorTableModel.class.getName()).log(Level.SEVERE, null, ex);
        } 
        return null;
    }

//    @Override
//    public void setDescription(String description) {
//
//        try {
//            String method = SET + field;
//            Method mt = getMethod(method);
//            mt.invoke(getObj(), convertParameter(mt.getParameterTypes()[0], description));
//        } catch (IllegalAccessException ex) {
//            Logger.getLogger(AbstractEditorTableModel.class.getName()).log(Level.SEVERE, null, ex);
//        } catch (IllegalArgumentException ex) {
//            Logger.getLogger(AbstractEditorTableModel.class.getName()).log(Level.SEVERE, null, ex);
//        } catch (InvocationTargetException ex) {
//            Logger.getLogger(AbstractEditorTableModel.class.getName()).log(Level.SEVERE, null, ex);
//        }
//
//    }
    public void setValue(Object description) {

        try {
            String method = SET + field;
            Method mt = getMethod(method);
            mt.invoke(getObj(), convertParameter(mt.getParameterTypes()[0], description));
        } catch (IllegalAccessException ex) {
            Logger.getLogger(AbstractEditorTableModel.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IllegalArgumentException ex) {
            Logger.getLogger(AbstractEditorTableModel.class.getName()).log(Level.SEVERE, null, ex);
        } catch (InvocationTargetException ex) {
            Logger.getLogger(AbstractEditorTableModel.class.getName()).log(Level.SEVERE, null, ex);
        }

    }

    private Method getMethod(String method) {
        for (Method mt : getObj().getClass().getMethods()) {
            if (mt.getName().equalsIgnoreCase(method)) {
                return mt;
            }
        }
        throw new IllegalArgumentException();
    }

    private Object convertParameter(Class clazz, Object value) {
        if(value == null)
            throw new IllegalArgumentException();
        if (clazz.getName().equals(value.getClass().getName())) {
            return value;
        } else if (!clazz.getName().equals(java.lang.String.class.getName())
                && value.getClass().getName().equals(java.lang.String.class.getName())) {
            try {
                Method method = clazz.getMethod("valueOf", java.lang.String.class);
                if (Modifier.isStatic(method.getModifiers())) {
                    try {
                        return method.invoke(null, value);
                    } catch (IllegalAccessException ex) {
                        Logger.getLogger(AbstractEditorTableModel.class.getName()).log(Level.SEVERE, null, ex);
                    } catch (IllegalArgumentException ex) {
                        Logger.getLogger(AbstractEditorTableModel.class.getName()).log(Level.SEVERE, null, ex);
                    } catch (InvocationTargetException ex) {
                        Logger.getLogger(AbstractEditorTableModel.class.getName()).log(Level.SEVERE, null, ex);
                    }
                }
            } catch (NoSuchMethodException ex) {
                Logger.getLogger(AbstractEditorTableModel.class.getName()).log(Level.SEVERE, null, ex);
            } catch (SecurityException ex) {
                Logger.getLogger(AbstractEditorTableModel.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        throw new IllegalArgumentException();
    }
}
